package com.lhkj.ct.wechat.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.enums.OperatorType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.meta.modules.psychology.model.bo.DimensionBo;
import com.lhkj.ct.meta.modules.psychology.model.dto.ProcessDto;
import com.lhkj.ct.meta.modules.psychology.model.dto.ScaleSubmitDto;
import com.lhkj.ct.meta.modules.psychology.model.vo.PsyRecordsVo;
import com.lhkj.ct.meta.modules.psychology.model.vo.ViewAnswerVo;
import com.lhkj.ct.meta.modules.psychology.service.PsyAnswerService;
import com.lhkj.ct.meta.modules.psychology.service.PsyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/wechat/psy/record")
@Api(tags = {"1.测验记录"})
public class WxMpEvaluationController {

    @Resource
    private PsyRecordService psyRecordService;

    @Resource
    private PsyAnswerService psyAnswerService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "测验列表")
    @GetMapping("list")
    public ActionResult<IPage<PsyRecordsVo>> selectPatUnitPage(PsyRecordsVo record, PageQuery pageQuery) {
        record.setPatId(ShiroUtil.getAuthUserInfo().getUserId());
        return ActionResult.success(psyRecordService.selectPagesByPatient(record, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取测验的量表，题目等信息", notes = "rId:报告id, info:量表信息, questions:题目, answerJson:答案, costTime:计时")
    @GetMapping("{allotId}/get")
    public ActionResult<Map<String, Object>> get(@PathVariable String allotId) {
        return ActionResult.success(psyRecordService.selectScaleWithQuestionsByAllotId(allotId, false));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查看作答情况")
    @GetMapping("{allotId}/view")
    public ActionResult<ViewAnswerVo> view(@PathVariable String allotId) {
        return ActionResult.success(psyRecordService.viewAnswer(allotId));
    }

    @PostMapping("submit")
    @ApiOperation(value = "作答提交")
    @Log(title = "测验提交", actionType = ActionType.INSERT, operatorType = OperatorType.MOBILE)
    public ActionResult<List<DimensionBo>> submit(@Valid @RequestBody ScaleSubmitDto scaleSubmitDto) {
        psyAnswerService.submit(scaleSubmitDto);
        return ActionResult.success();
    }

    @PostMapping("process")
    @ApiOperation(value = "保存作答进度")
    @Log(title = "测验作答", actionType = ActionType.UPDATE, operatorType = OperatorType.MOBILE)
    public ActionResult<Void> saveProcess(@Validated @RequestBody ProcessDto process) {
        return ActionResult.judge(psyAnswerService.saveProcess(process));
    }
}
